import { message } from 'antd';
import type { ITabMenuListType } from './type';
import { serverUrl } from '../../../../config/urlConfig';

// 处理三级tab菜单列表数据
export const getTabMenuOptions = (data?: ITabMenuListType[]): any => {
  const menuList: any[] = [];
  const leftNodes: any[] = [];
  const _data = data?.map((a) => {
    const childrenOne = a?.secondMenuList?.map((b) => {
      const childrenTwo: any = b?.tabMenuList?.map((c) => {
        const obj: any = {
          firstMenuId: a?.firstMenuId,
          firstMenuName: a?.name,
          secondMenuId: b?.secondMenuId,
          secondMenuName: b?.name,
          tabMenuId: c?.tabMenuId,
          tabMenuName: c?.tabMenuName,
        };
        const left: any = {
          key: c?.tabMenuId,
          title: c?.tabMenuName,
          value: c?.tabMenuId,
          label: c?.tabMenuName,
          menu: obj,
          parentKey: b?.secondMenuId,
        };
        leftNodes.push(left);
        menuList.push(obj);
        return {
          key: c?.tabMenuId,
          title: c?.tabMenuName,
          value: c?.tabMenuId,
          label: c?.tabMenuName,
        };
      });
      if (!b?.tabMenuList?.length) {
        const obj: any = {
          firstMenuId: a?.firstMenuId,
          firstMenuName: a?.name,
          secondMenuId: b?.secondMenuId,
          secondMenuName: b?.name,
        };
        const left = {
          key: b?.secondMenuId,
          title: b?.name,
          value: b?.secondMenuId,
          label: b?.name,
          menu: obj,
          parentKey: a?.firstMenuId,
        };
        menuList.push(obj);
        leftNodes.push(left);
      }
      return {
        key: b?.secondMenuId,
        title: b?.name,
        value: b?.secondMenuId,
        label: b?.name,
        children: childrenTwo,
      };
    });
    if (!a?.secondMenuList?.length) {
      const obj: any = {
        firstMenuId: a?.firstMenuId,
        firstMenuName: a?.name,
      };
      const left: any = {
        key: a?.firstMenuId,
        title: a?.name,
        value: a?.firstMenuId,
        label: a?.name,
        menu: obj,
        parentKey: null,
      };
      menuList.push(obj);
      leftNodes.push(left);
    }
    return {
      key: a?.firstMenuId,
      title: a?.name,
      value: a?.firstMenuId,
      label: a?.name,
      children: childrenOne,
    };
  });
  return {
    modalData: _data,
    menuList,
    leftNodes,
  };
};

// 判断该用户是否有权限
export const havePermissions = (buttonType: string, buttonPermissions: any) => {
  const hasSave =
    buttonPermissions?.filter((item: any) => item.menuCode === buttonType)?.length > 0;
  if (!hasSave) {
    message.info('请联系管理员获取相关权限');
    return false;
  } else {
    return true;
  }
};

// 数据保留小数位
export const ToFixed = (data: any, persion = 3) => {
  if (!data) return data;
  const dot = String(data).indexOf('.');
  let num = Number(data);
  if (dot !== -1) {
    const dotCnt = String(data).substring(dot + 1, data.length);
    if (dotCnt.length > persion) {
      num = Number(Number(data).toFixed(persion));
    } else {
      num = Number(data);
    }
  }
  return num;
};

// 下载
export const handleDownload = async (
  obj: { annexPath?: string; annexId?: string },
  system: boolean,
) => {
  if (system) {
    // 下载
    // @ts-ignore
    const client = new OSS({
      region: 'oss-cn-beijing',
      accessKeyId: 'LTAI4G7w6tvFNPQ4q2hcHcD6',
      accessKeySecret: 'ZgALLEnmeqjjcTxlJnDEKKfJ6R5GUy',
      bucket: 'tianrun',
    });
    const url = client.signatureUrl(obj?.annexPath, {
      expires: 3600,
      response: {
        'content-disposition': 'attachment; filename="' + obj?.annexPath + '"',
      },
    });
    window.open(url);
  } else {
    window.open(`${serverUrl}/annex/annex/downLoadFile/${obj?.annexId}`);
  }
};

export const getFileUrl = (file: any = {}, isDownLoad = false) => {
  // isDownLoad 是否强制下载
  const signatureConf: any = {
    expires: 3600,
  };
  if (isDownLoad) {
    signatureConf.response = {
      'content-disposition': `attachment; filename="${file?.fileName || ''}"`,
    };
  }
  // @ts-ignore
  const url = new OSS({
    region: 'oss-cn-beijing',
    accessKeyId: 'LTAI4G7w6tvFNPQ4q2hcHcD6',
    accessKeySecret: 'ZgALLEnmeqjjcTxlJnDEKKfJ6R5GUy',
    bucket: 'tianrun',
  }).signatureUrl(file.filePath, signatureConf);
  return Promise.resolve(url);
};

// 预览
export const handelPreview = async (obj: any, system: boolean) => {
  const urlPrefix = 'https://view.officeapps.live.com/op/view.aspx';
  if (system) {
    //系统
    let _purl = await getFileUrl(obj);
    if (
      obj?.fileName?.includes('.doc') ||
      obj?.fileName?.includes('.xls') ||
      obj?.fileName?.includes('.lsx')
    ) {
      _purl = `${urlPrefix}?src=${encodeURIComponent(_purl)}`;
    }
    return _purl;
  } else {
    const url = `${serverUrl}/annex/annex/downLoadFile/${obj?.fileId || ''}?mandatory=false`;
    if (
      obj.fileName.includes('.mp4') ||
      obj.fileName.includes('.pdf') ||
      obj.fileName.includes('.png') ||
      obj.fileName.includes('.jpg')
    ) {
      return url;
    } else {
      return `${urlPrefix}?src=${serverUrl}/annex/annex/downLoadFile/${obj?.fileId || ''}`;
    }
  }
};

export const USER_DATA = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
